package com.blog.mapper;

import com.blog.pojo.Comment;
import com.blog.pojo.CommentWithArticleTitleVO;
import com.blog.pojo.CommentWithUsernameAndImageVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;


import java.util.List;

@Mapper
public interface CommentMapper {

    /**
     * 分页查看所有评论
     * @return 评论视图列表
     */
    @Select("select c.*, a.title as articleTitle from comment c join article a on c.article_id = a.id " +
            "order by c.created desc ")
    List<CommentWithArticleTitleVO> listAll();

    /**
     * 分页查看用户收到的所有评论
     * @param id 用户id
     * @return 评论视图列表
     */
    @Select("select c.*, a.title as articleTitle from comment c join article a on c.article_id = a.id " +
            "where a.author_id = #{id} " +
            "order by c.created desc")
    List<CommentWithArticleTitleVO> listByUserId(Integer id);

    /**
     * 分页查看一篇文章的所有评论
     * @param id 文章id
     * @return 评论视图列表
     */
    @Select("select c.*, u.username as username, u.image as image from comment c join user u on c.user_id = u.id " +
            "where c.article_id = #{id} " +
            "order by c.created desc")
    List<CommentWithUsernameAndImageVO> listByArticleId(Integer id);

    /**
     * 向评论表中加入表项
     * @param comment 评论实体类
     */
    @Insert("insert into comment (article_id, user_id, content, created) values (#{articleId}, #{userId}, #{content}, #{created})")
    void insert(Comment comment);

    /**
     * 根据id删除评论
     * @param id 评论id
     */
    @Delete("delete from comment where id = #{id}")
    void deleteById(Integer id);


}
